import { Packet } from 'packet-model.js';
var packet = new Packet();
Page({

  data: {
    statusType: ["发出的红包", "收到的红包"],
    currentTpye: 0,
    page: 1,
    log:[],
    hiddenNullData: true,
    hiddenNomore: true,
    hiddenLoading: true,
    onLoading: false,
    nomore: false,
  },
  onLoad: function (options) {
    var index = options.index;
    this.sendRecord();
  },
  sendRecord: function () {
    //是否正在加载数据或已加载完全部数据
    if (this.data.onLoading || this.data.nomore) {
      return false;
    }
    var that = this;
    var args = {
      page: this.data.page,
      num:15
    }
    //显示正在加载中
    this.setData({
      hiddenLoading: false
    })
    this.data.onLoading = true;
    packet.sendRecord(args, (data) => {
      var items = data.data.items;
      if(items){
        items.forEach((item, index) => {
          var info = item.info;
          items[index].info = JSON.parse(info);
          items[index].change_money = Math.abs(item.change_money)
        })
      }
      data.data.items = items;
      that.data.onLoading = false;
      var log = that.data.log.concat(data.data.items);
      that.setData({
        log
      })
      //隐藏正在加载中
      that.setData({
        hiddenLoading: true
      })
      if (log.length == 0) {
        //显示没有数据
        that.setData({
          hiddenNullData: false
        })
      }
      if (that.data.page >= data.data._meta.pageCount) {
        that.data.nomore = true;
        if (that.data.page > 1) {
          //显示没有更多
          that.setData({
            hiddenNomore: false
          })
        }
      }

    });
  },
  receiveRecord: function () {
    //是否正在加载数据或已加载完全部数据
    if (this.data.onLoading || this.data.nomore) {
      return false;
    }
    var that = this;
    var args = {
      page: this.data.page,
      num: 15
    }
    //显示正在加载中
    this.setData({
      hiddenLoading: false
    })
    this.data.onLoading = true;
    packet.receiveRecord(args, (data) => {
      var items = data.data.items;
      if (items) {
        items.forEach((item, index) => {
          var info = item.info;
          items[index].info = JSON.parse(info);
          items[index].change_money = Math.abs(item.change_money)
        })
      }
      data.data.items = items;
      that.data.onLoading = false;
      var log = that.data.log.concat(data.data.items);
      that.setData({
        log
      })
      //隐藏正在加载中
      that.setData({
        hiddenLoading: true
      })
      if (log.length == 0) {
        //显示没有数据
        that.setData({
          hiddenNullData: false
        })
      }
      if (that.data.page >= data.data._meta.pageCount) {
        that.data.nomore = true;
        if (that.data.page > 1) {
          //显示没有更多
          that.setData({
            hiddenNomore: false
          })
        }
      }

    });
  },
  statusTap: function (e) {
    var curType = e.currentTarget.dataset.index;
    this.data.currentTpye = curType
    this.setData({
      hiddenNullData : true,
      currentTpye: curType,
      log : [] ,//重置数据
      
    });
    //重置数据
    this.data.page = 1;
    this.data.nomore = false;
    
    //切换接口
    if (curType == 0){
      this.sendRecord();
    } else if (curType == 1){
      this.receiveRecord();
    }
  },
  //去到详情页面
  toDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    if(!id){
      return false;
    }
    wx.navigateTo({
      url: 'detail?id='+id
    });
  },
  onReachBottom: function () {
    if (this.data.nomore) {
      this.setData({
        hiddenNomore: false
      })
      return false;
    }
    this.data.page ++;
    if (this.data.currentTpye == 0) {
      this.sendRecord();
    } else if (this.data.currentTpye == 1) {
      this.receiveRecord();
    }
  },
})